home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Memory. How is it organised?
- Date: 4 Mar 1996 09:34:32 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hf9j8INNko4@keats.ugrad.cs.ubc.ca>
- References: <4hf5gs$1f7@news.mistral.co.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4hf5gs$1f7@news.mistral.co.uk>,
- Mike Barnard <mikebarnard@mistral.co.uk> wrote:
- >Hi all.
- >
- >I want to find out as much as possible about the stack and
- >the heap. I know some of this is machine specific, but not
- >all of it.
-
- ALL of it is machine specific, make no mistake.
-
- >Suppose I did this?
- >
- >struct tag
- > {
- > // Variables taking 100 bytes, for an example
- > };
- >
- >struct tag arrayname[50];
- >
- >What happens? Is 5000 bytes of memory reserved for the
- >contents of the array? Where? Stack, heap or somewhere else?
-
- That depends on where you declared the storage. If it is declared inside a
- function body, then the storage is part of the activation record of the
- function. Such variables are called ``automatic'' in C. Note my use of the the
- compound word ``activation record'' to avoid saying ``stack''.
-
- If you declare it outside of a function, or if you label it ``static'' inside a
- function, it is conceptually found in a global memory area.
-
- >Or does the compiler just make a "note" that it will be
- >needed then allocate some memory when the array(s) are
- >initialised. Again, where? (What is the definition of stack
- >or heap?)
-
- Static variables are conceptually allocated before the program starts, and
- initialized the first time the block in which they are found is entered. Or, if
- they are outside of any function, they are initialized before execution begins.
- In actual implementations, typically what happens is that all statics are
- initialized as a big data block that forms part of your executable file. It is
- read (or page faulted) into memory at the correct address so that the program
- can access the variables.
-
- A stack is just a form of dynamic allocation for activation records. Since most
- languages have nested semantics for opening and closing activation records,
- they are adequately modelled with a stack mechanism.
-
- >Should I malloc the array? How? That is to say; malloc as a
- >part of the array definition, or seperately looping through
- >each member of the array and mallocing it after it's been
- >defined?
-
- No you don't have to malloc it. Arrays can't be malloced: they are created in
- the activation record of your procedure or in static storage. Malloc can only
- be used for getting contiguous regions of memory referenced by a pointer
- variable. You can pretend that such an area is an array, but technically it is
- not in the strict C sense.
-
- The statement ``struct tag arrayname[50];'' ensures that arrayname is an
- instantiated array with 50 elements.
-
- If you declare this within a function without the ``static'' storage type
- keyword, then each recursive invocation of the function gets its own object
- called ``arrayname''. On the other hand, if you declare it static or put it
- outside of a function, only one copy will exist in the program.
-
- >Someone said on an irc channel about C++ that malloc is
- >obsolete. "Use the new command" he said. Then logged off!!!
-
- That person was a fool. Disregard such unenlightened advice. malloc() is a
- standard defined function of ANSI C. It is no better or worse than the new
- operator of C++. And C++ is not even standardized yet, whereas malloc() appears
- in an ISO standards document, which effectively gives it the upper hand.
- New is an operator, and not a command. Anyone who uses the word ``command'' in
- the place of ``operator'', ``expression'', ``function'' or ``statement'' is a
- dBase IV programmer in disguise and should be ignored when giving C advice.
-
- The malloc() function is used for allocating memory that cannot be allocated
- statically (for example, because the amount needed cannot be stated in terms of
- fixed compile-time limits) but which, unlike memory allocated in nested
- activation records, has to survive the exit of the function in which
- it is created; i.e. malloc() memory exists until it is explictly removed with
- te free() function. You can free() memory in one function that was malloc()ed
- in an other, provided that you communicate the correct pointer from one to the
- other.
-
- If you wanted to allocate an ``array'' of structures with malloc, you would
- need a pointer variable to hold that address. The pointer has to be of a
- suitable type for the structucture:
-
- struct tag *tagptr;
-
- tagptr = malloc(50 * sizeof(struct tag));
-
- if (tagptr == 0) {
- printf("malloc() failed!\n");
- exit(1);
- }
-
- The memory set up by malloc() has undefined contents, so you have to initialize
- it before use. The syntax for accessing the elements of the ``array'' is
- purposely the same as accessing a static or automatic array. You just say
- ``tagptr[3]'' to reference the fourth structure, for instance.
-
- >I presume there's something in C++ that relates to this, but
- >I don't feel ready for OO yet. I want to learn to walk, not
- >run.
-
- More like, you have to learn to walk before you can fall on your butt. :)
-
- There is plenty you can do in the ordinary ANSI C language that can keep you
- busy. Incidentally, object-oriented design and implementation is certainly
- possible in C, but it requires programming maturity and discipline.
- --
-
-